<template>
  <view class="flex-col page">
    <view class="flex-row justify-between group_2">
      <view class="flex-col group_4">
        <text class="self-start text_2">👋 Hello!</text>
        <text class="self-stretch text_3 mt-9">WelCome</text>
      </view>
      <view class="flex-col justify-start items-start self-start relative group_3">
        <image
          class="image_4"
          src="https://joyballet.oss-cn-beijing.aliyuncs.com/eyemanager/wechat/basicprofile.png"
        />
      </view>
    </view>
    <view class="flex-row justify-between section_2">
      <view class="flex-row items-center">
        <image
          class="shrink-0 image_5"
          src="https://joyballet.oss-cn-beijing.aliyuncs.com/eyemanager/wechat/search_point.png"
        />
        <input class="text_4 ml-11" v-model="searchText" placeholder="有什么问题需要帮助吗"></input>
      </view>
      <image
        class="image_5 image_6"
        @click="handleSearch"
        src="https://joyballet.oss-cn-beijing.aliyuncs.com/eyemanager/wechat/search_btn.png"
      />
    </view>
    <view class="flex-row equal-division">
      <view class="flex-col justify-start items-start equal-division-item image-wrapper" @click="uploadReport()">
        <view class="flex-row items-center">
          <image
            class="image_8"
            src="https://joyballet.oss-cn-beijing.aliyuncs.com/eyemanager/wechat/ask.png"
          />
          <text class="report-title ml-8">报告解读</text>
        </view>
        <text class="report-desc">智能细致解读</text>
        <text class="report-desc">指标全面讲解</text>
      </view>
      <view class="flex-col justify-start items-start equal-division-item image-wrapper_2 ml-19" @click="gotoAsk()">
        <view class="flex-row items-center">
          <image
            class="image_7"
            src="https://joyballet.oss-cn-beijing.aliyuncs.com/eyemanager/wechat/docter.png"
          />
          <text class="report-title ml-8">专家问诊</text>
        </view>
        <text class="report-desc">24小时在线</text>
        <text class="report-desc">专业级的问诊</text>
      </view>
    </view>
    <view class="flex-row justify-between section_3">
      <view class="flex-col self-start group_5">
        <text class="self-start font">你身边的</text>
        <text class="self-start font">儿童眼科专家</text>
        <text class="self-stretch text_5">报告解读 | 眼科医疗问诊 |治疗方案</text>
      </view>
      <image
        class="image_9"
        src="https://joyballet.oss-cn-beijing.aliyuncs.com/eyemanager/wechat/eye_teacher1.png"
      />
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      report: '',
      userId: 'user123',
      searchText: '', // 添加搜索文本
    };
  },

  // 添加分享到微信好友
  onShareAppMessage(res) {
    return {
      title: '你身边的儿童眼科专家',
      path: '/pages/shouye/shouye',
      imageUrl: 'https://joyballet.oss-cn-beijing.aliyuncs.com/eyemanager/wechat/eye_teacher1.png',
      success(res) {
        uni.showToast({
          title: '分享成功'
        })
      },
      fail(res) {
        uni.showToast({
          title: '分享失败',
          icon: 'none'
        })
      }
    }
  },

  // 添加分享到朋友圈
  onShareTimeline() {
    return {
      title: '你身边的儿童眼科专家',
      query: '',
      imageUrl: 'https://joyballet.oss-cn-beijing.aliyuncs.com/eyemanager/wechat/eye_teacher1.png'
    }
  },

  methods: {
    // 添加搜索处理方法
    handleSearch() {
      if (!this.searchText.trim()) {
        uni.showToast({
          title: '请输入问题内容',
          icon: 'none'
        });
        return;
      }
      
      uni.navigateTo({
        url: `/pages/huaban_18/huaban_18?question=${encodeURIComponent(this.searchText)}`
      });
    },
    // 跳转到 Ask 页面
    gotoAsk() {
      uni.navigateTo({
        url: '/pages/huaban_18/huaban_18'
      })
    },
    // 上传检查报告
    uploadReport(){
      let that = this
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success (res) {
          const tempFilePaths = res.tempFilePaths
          that.$tools.uploadFile(tempFilePaths, function(res){
            console.log(res)
            that.report = 'https://joystudenttask.oss-cn-beijing.aliyuncs.com/' + res
            // 上传成功后调用 AI 接口
            that.callAIService(that.report)
          })
        }
      })
    },
    // 调用 AI 服务接口
    callAIService(imageUrl) {
      uni.navigateTo({
        url: `/pages/huaban_18/huaban_18?question=${encodeURIComponent(imageUrl)}&report=1`
      });
      // 显示加载提示
      // uni.showLoading({
      //   title: '正在分析报告，请耐心等待...',
      //   mask: true
      // })
      
      // const requestTask = uni.request({
      //   url: 'https://api.coze.cn/v1/workflow/run',
      //   method: 'POST',
      //   timeout: 120000, // 设置超时时间为 120 秒
      //   header: {
      //     'X-Source': 'openapi',
      //     'Content-Type': 'application/json',
      //     'Authorization': 'Bearer pat_tM6LRVs0WFiuDPZ9o9qskfl7PzeVUZXGFLg3I8GJj7Z5k0NYwzwaDNySjH1k7Iq5'
      //   },
      //   data: {
      //     "parameters": {
      //       "report_img_url": imageUrl
      //     },
      //     "workflow_id": "7476471958684172324"
      //   },
      //   success: (response) => {
      //     uni.hideLoading()
      //     if (response.statusCode === 200) {
      //       console.log('AI 分析结果：', response.data)
      //       uni.navigateTo({
      //         url: `/pages/Messages/Messages?aiResponse=${encodeURIComponent(response.data.data)}`
      //       })
      //     } else {
      //       uni.showToast({
      //         title: '分析失败，请重试',
      //         icon: 'none'
      //       })
      //     }
      //   },
      //   fail: (error) => {
      //     uni.hideLoading()
      //     console.error('调用 AI 服务失败：', error)
      //     let errorMsg = '分析失败，请重试'
      //     if (error.errMsg && error.errMsg.includes('timeout')) {
      //       errorMsg = '分析超时，请重试'
      //     }
      //     uni.showToast({
      //       title: errorMsg,
      //       icon: 'none',
      //       duration: 2000
      //     })
      //   },
      //   complete: () => {
      //     // 确保在任何情况下都隐藏 loading
      //     setTimeout(() => {
      //       uni.hideLoading()
      //     }, 500)
      //   }
      // })

      // // 添加中断处理
      // setTimeout(() => {
      //   if (requestTask) {
      //     requestTask.abort()
      //     uni.hideLoading()
      //     uni.showToast({
      //       title: '请求时间过长，已自动中断',
      //       icon: 'none',
      //       duration: 2000
      //     })
      //   }
      // }, 120000) // 120 秒后自动中断
    }
  }
};
</script>

<style scoped lang="css">
  .ml-5 {
    margin-left: 5px;
  }
  .mt-9 {
    margin-top: 9px;
  }
  .ml-11 {
    margin-left: 11px;
  }
  .ml-19 {
    margin-left: 19px;
  }
  .ml-99 {
    margin-left: 99px;
  }
  .page {
    padding: 14px 14px 10px 33px;
    background-color: #ffffff;
    width: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    height: 100%;
  }
  .group {
    padding-left: 4px;
  }
  .text {
    color: #141023;
    font-size: 15px;
    font-family: HarmonyOSSansSC;
    font-weight: 300;
    line-height: 11.5px;
  }
  .image {
    width: 17px;
    height: 11px;
  }
  .image_2 {
    width: 15px;
    height: 11px;
  }
  .image_3 {
    width: 24px;
    height: 11.5px;
  }
  .group_2 {
    margin-top: 30px;
    padding: 0 2px;
  }
  .group_4 {
    margin-top: 2px;
    width: 116px;
  }
  .text_2 {
    margin-left: 24px;
    color: #253141;
    font-size: 16px;
    font-family: NunitoSans;
    font-weight: 600;
    line-height: 18px;
  }
  .text_3 {
    color: #253141;
    font-size: 27px;
    font-family: NunitoSans;
    font-weight: 700;
    line-height: 35.5px;
  }
  .group_3 {
    margin-right: 38px;
    width: 57px;
  }
  .image_4 {
    border-radius: 18px;
    width: 48px;
    height: 48px;
  }
  .section {
    background-color: #43cf7c;
    border-radius: 50%;
    width: 18px;
    height: 18px;
    border-left: solid 3px #ffffff;
    border-right: solid 3px #ffffff;
    border-top: solid 3px #ffffff;
    border-bottom: solid 3px #ffffff;
  }
  .pos {
    position: absolute;
    right: 0;
    top: 0;
  }
  .section_2 {
    margin: 38px 4px 0 6px;
    padding: 18px 15px;
    background-color: #eef6fc;
    border-radius: 18px;
  }
  .image_5 {
    width: 20px;
    height: 20px;
  }
  .text_4 {
    color: #8aa0bc;
    font-size: 12px;
    font-family: NunitoSans;
    line-height: 11px;
  }
  .image_6 {
    margin-right: 5px;
  }
  .equal-division {
    margin-right: 8px;
    margin-top: 56px;
  }
  .equal-division-item {
    flex: 1 1 150px;
  }
  .image-wrapper {
    padding: 22px 0 78px;
    background-color: #d6f6ff;
    border-radius: 20px;
    height: 132px;
  }
  .image_8 {
    margin-left: 20px;
    width: 28px;
    height: 32px;
  }
  .image-wrapper_2 {
    padding: 18px 0 78px;
    background-color: #dcedf9;
    border-radius: 20px;
    height: 132px;
  }
  .image_7 {
    margin-left: 22px;
    width: 30px;
    height: 35px;
  }
  .section_3 {
    margin-right: 8px;
    margin-top: 25px;
    padding-left: 24px;
    overflow: hidden;
    border-radius: 28px;
    background-color: #dcedf9;
    height: 169.5px;
  }
  .group_5 {
    margin-top: 38px;
    width: 142px;
  }
  .font {
    font-size: 20px;
    font-family: SourceHanSerifCN;
    line-height: 27px;
    color: #0e1012;
  }
  .text_5 {
    margin-top: 8px;
    color: #4a545e;
    font-size: 11px;
    font-family: NunitoSans;
    line-height: 15px;
  }
  .image_9 {
    margin-bottom: -14px;
    width: 177px;  /* 调整宽度 */
    height: 182px; /* 保持原有高度 */
    object-fit: cover; /* 保持图片比例 */
  }
  .section_4 {
    margin: 139px 13px 0 4px;
    padding: 22px 22px 14px 25px;
    background-color: #ffffff;
    border-radius: 16px;
    filter: drop-shadow(5px 12px 10px #dadae829);
    overflow: hidden;
  }
  .equal-division_2 {
    align-self: stretch;
  }
  .equal-division-item_2 {
    width: 22.5px;
    height: 22.5px;
  }
  .section_5 {
    margin-left: 8px;
    background-color: #00afff;
    border-radius: 50%;
    width: 4px;
    height: 4px;
  }
  .report-title {
    color: #253141;
    font-size: 16px;
    font-family: NunitoSans;
    font-weight: 600;
  }
  .report-desc {
    margin-left: 20px;
    margin-top: 8px;
    color: #8aa0bc;
    font-size: 12px;
    font-family: NunitoSans;
    line-height: 16px;
  }
  .ml-8 {
    margin-left: 8px;
  }
</style>